package com.comleader.ldmapdownload.controller;

import cn.hutool.json.JSONUtil;
import com.comleader.ldmapdownload.bean.LocationEntity;
import com.comleader.ldmapdownload.bean.LocationParamsVo;
import com.comleader.ldmapdownload.bean.StayLocationEntity;
import com.comleader.ldmapdownload.util.ConfigProperties;
import com.comleader.ldmapdownload.util.ElasticsearchUtil;
import com.comleader.ldmapdownload.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghang
 * @date 2020-09-25 11:13
 * @description 定位、轨迹等服务的相关接口
 */
@CrossOrigin
@RestController
@RequestMapping("/location")
@SuppressWarnings("all")
public class LocationController {

    @Autowired
    private ConfigProperties configProperties;

    @Autowired
    private ElasticsearchUtil esUtil;

    /**
     * @return
     * @description 获取当前位置的接口，定位服务
     * @author zhanghang
     * @date 2020-09-25 11:16:21
     **/
    @RequestMapping("/getNow")
    public StayLocationEntity getNowLocation(@RequestBody String data) {

        StayLocationEntity stayLocation = new StayLocationEntity();
//        // 模拟数据
//        stayLocation = new StayLocationEntity();
//        stayLocation.setUserNum("13014531712");
//        stayLocation.setStayTime("2小时33秒");
//        stayLocation.setStartTime("2020-9-27 10:08:30");
//        stayLocation.setAddress("金水区希尔顿欢朋酒店");
//        stayLocation.setLat(34.76023000);
//        stayLocation.setLng(113.58833300);
//        return stayLocation;
        LocationParamsVo locationParamsVo = JSONUtil.toBean(data, LocationParamsVo.class);
        try {
            if (locationParamsVo == null){
                return null;
            }
            // 查询es
            List<LocationEntity> entities = esUtil.searchByPhone(locationParamsVo.getUserNum());
            if (entities.size() > 0) {
                LocationEntity location = entities.get(entities.size() - 1);
                stayLocation.setUserNum(location.getC_usernum());
                stayLocation.setAddress(location.getC_uli_addr());
                stayLocation.setLat(location.getC_lat());
                stayLocation.setLng(location.getC_lng());
                // 出现在该位置的时间
                stayLocation.setStartTime(TimeUtils.timeStampToString(location.getC_timestamp() * 1000));
                // 停留时长
                stayLocation.setStayTime(TimeUtils.toDuration((System.currentTimeMillis() - (location.getC_timestamp() * 1000))));
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return stayLocation;
    }


    /**
     * @param data
     * @description
     * @author 获取历史轨迹停留的地点
     * @date 2020-09-25 11:17:22
     **/
    @RequestMapping("/getTracksByStay")
    public List<StayLocationEntity> getTracksByStay(@RequestBody String data) {
        List<StayLocationEntity> resList = new ArrayList<>();
//        StayLocationEntity stayLocation = new StayLocationEntity();
//        stayLocation.setUserNum("13014531712");
//        stayLocation.setStayTime("2小时33秒");
//        stayLocation.setStartTime("2020-9-27 10:08:30");
//        stayLocation.setAddress("金水区希尔。顿欢朋酒店");
//        stayLocation.setLat(34.838604318635014);
//        stayLocation.setLng(113.57666015625001);
//        resList.add(stayLocation);
//        StayLocationEntity stayLocation1 = new StayLocationEntity();
//        stayLocation1 = new StayLocationEntity();
//        stayLocation1.setUserNum("13014531712");
//        stayLocation1.setStayTime("2小时33秒");
//        stayLocation1.setStartTime("2020-9-27 10:08:30");
//        stayLocation1.setAddress("金水区希尔顿欢朋酒店");
//        stayLocation1.setLat(34.72581233927868);
//        stayLocation1.setLng(113.74969482421876);
//        resList.add(stayLocation1);
//        StayLocationEntity stayLocation2 = new StayLocationEntity();
//        stayLocation2 = new StayLocationEntity();
//        stayLocation2.setUserNum("13014531712");
//        stayLocation2.setStayTime("2小时33秒");
//        stayLocation2.setStartTime("2020-9-27 10:08:30");
//        stayLocation2.setAddress("金水区希尔顿欢朋酒店");
//        stayLocation2.setLat(34.72681233927868);
//        stayLocation2.setLng(113.75069482421876);
//        resList.add(stayLocation2);
//        return resList;
        try {
            LocationParamsVo locationParamsVo = JSONUtil.toBean(data, LocationParamsVo.class);
            if (locationParamsVo == null){
                return null;
            }
            // 查询出的坐标点的集合(查询es)
//        List<LocationEntity> entities = new ArrayList<>();
            List<LocationEntity> entities = esUtil.searchByPhone(locationParamsVo.getUserNum());
            resList = new ArrayList<>();

            // 如果用户位置信息没有变动,就不去处理
            // 如果用户位置变动了,判断是否处于移动状态(停留时间是否小于10分钟),如果移动中就不做处理,只保留停留的
            for (int i = 0; i < entities.size(); i++) {
                LocationEntity location = entities.get(i);
                StayLocationEntity stayLocation = new StayLocationEntity();

                // 如果是最后一个位置点,可以直接处理结束
                if (i == entities.size() - 1) {
                    stayLocation.setUserNum(location.getC_usernum());
                    stayLocation.setAddress(location.getC_uli_addr());
                    stayLocation.setLat(location.getC_lat());
                    stayLocation.setLng(location.getC_lng());
                    // 出现在该位置的时间
                    stayLocation.setStartTime(TimeUtils.timeStampToString(location.getC_timestamp() * 1000));
                    // 停留时长
                    stayLocation.setStayTime(TimeUtils.toDuration((System.currentTimeMillis() - location.getC_timestamp() * 1000)));
                    resList.add(stayLocation);
                    break;
                }

                // 如果不是最后一个位置点,与下一个位置点进行比较,判断是否符合驻留
                LocationEntity nextLocation = entities.get(i + 1);
                // 判断是否发生移动
                if (nextLocation.getC_uli_addr().equals(location.getC_uli_addr())) {
                    // 没有发生移动则跳过
                    continue;
                }

                // 发生移动了判断驻留时间是否超过默认驻留时长
                if (nextLocation.getC_timestamp() - location.getC_timestamp() >= configProperties.getLocationStayTime() * 60) {
                    // 超过了默认时长认为驻留了
                    stayLocation.setUserNum(location.getC_usernum());
                    stayLocation.setAddress(location.getC_uli_addr());
                    stayLocation.setLat(location.getC_lat());
                    stayLocation.setLng(location.getC_lng());
                    // 出现在该位置的时间
                    stayLocation.setStartTime(TimeUtils.timeStampToString(location.getC_timestamp() * 1000));
                    // 停留时长
                    stayLocation.setStayTime(TimeUtils.toDuration((nextLocation.getC_timestamp() - location.getC_timestamp()) * 1000));
                    resList.add(stayLocation);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return resList;
    }

    /**
     * @param data
     * @return
     * @description
     * @author 获取历史轨迹所有的地点
     * @date 2020-09-25 11:17:22
     **/
    @RequestMapping("/getAllTracks")
    public List<StayLocationEntity> getAllTracks(@RequestBody String data) {
        LocationParamsVo locationParamsVo = JSONUtil.toBean(data, LocationParamsVo.class);

        List<StayLocationEntity> resultList = new ArrayList<>();

        return resultList;
    }


}
